import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *
from PySide6.QtSql import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        self.dbFile=os.path.join(self.parent.getAppDir(),'test.db')
        db=QSqlDatabase().addDatabase("QSQLITE")
        db.setDatabaseName(self.dbFile)
        layout = QGridLayout()       
        
        normalGroupBox=QGroupBox('Normal Model')
        normalLayout=QHBoxLayout()
        self.normalTableView=QTableView()
        self.normalModel=QSqlTableModel(db=db)
        
        normalLayout.addWidget(self.normalTableView)
        normalGroupBox.setLayout(normalLayout)

        sortGroupBox=QGroupBox('Sort/Filter Model')
        sortLayout=QGridLayout()

        self.sortTableView=QTableView()
        self.sortModel=QSortFilterProxyModel()
        
        sortLayout.addWidget(self.sortTableView,0,0,1,2)        

        sortLayout.addWidget(QLabel('FilterSyntax:'),1,0)
        self.filterSyntaxCombox=QComboBox()        
        self.filterSyntaxCombox.addItems(['FixedString','Wildcard','RegularExpression'])    
        self.filterSyntaxCombox.currentIndexChanged.connect(self.FilterSyntaxChanged)
        sortLayout.addWidget(self.filterSyntaxCombox,1,1)

        sortLayout.addWidget(QLabel('FilterColumn:'),2,0)
        self.filterColumnCombox=QComboBox()    
        self.filterColumnCombox.addItems(['Id','Name','City'])
        self.filterColumnCombox.currentIndexChanged.connect(self.FilterColumnChanged)
        sortLayout.addWidget(self.filterColumnCombox,2,1)

        sortLayout.addWidget(QLabel('FilterText:'),3,0)
        self.filterTextEdit=QLineEdit()
        self.filterTextEdit.textChanged.connect(self.FilterTextChanged)
        sortLayout.addWidget(self.filterTextEdit,3,1)

        sortLayout.addWidget(QLabel('SortOrder:'),4,0)
        self.sortOrderCombox=QComboBox()
        self.sortOrderCombox.addItems(Qt.SortOrder.__members__.keys())
        self.sortOrderCombox.currentTextChanged.connect(self.OrderChanged)
        sortLayout.addWidget(self.sortOrderCombox,4,1)

        self.ignoreCheckbox=QCheckBox('Ignore Case')
        sortLayout.addWidget(self.ignoreCheckbox,5,1)

        sortGroupBox.setLayout(sortLayout)

        layout.addWidget(normalGroupBox,0,0)
        layout.addWidget(sortGroupBox,1,0)

        self.setLayout(layout)
        self.LoadData()
    def LoadData(self):
        self.normalModel.database().open()
        self.normalModel.setTable('User')
        self.normalModel.select()#执行查询
        self.normalModel.database().close()

        self.normalTableView.setModel(self.normalModel)
        self.sortModel.setSourceModel(self.normalModel)
        self.sortTableView.setModel(self.sortModel)
    def FilterSyntaxChanged(self,index):
        pass
    def FilterColumnChanged(self,index):
        sortOrder = Qt.SortOrder[self.sortOrderCombox.currentText()]        
        self.sortModel.sort(index,sortOrder)        
        self.parent.OnShowMessage(f'Set Sort Column={index} Order={sortOrder}')
    def OrderChanged(self,text):
        sortOrder = Qt.SortOrder[text]
        index=self.filterColumnCombox.currentIndex()
        self.sortModel.sort(index,sortOrder)        
        self.parent.OnShowMessage(f'Set Sort Column={index} Order={sortOrder}')
    
    def FilterTextChanged(self):
        key=self.filterTextEdit.text()        
        index=self.filterColumnCombox.currentIndex()
        self.sortModel.setFilterKeyColumn(index)

        if self.ignoreCheckbox.checkState()==Qt.CheckState.Checked:
            self.sortModel.setFilterCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)
        else:
            self.sortModel.setFilterCaseSensitivity(Qt.CaseSensitivity.CaseSensitive)
        filterSyntax=self.filterSyntaxCombox.currentText()
        #清除过滤条件直接传空字符串即可
        if filterSyntax=='FixedString':
            self.sortModel.setFilterFixedString(key)
        elif filterSyntax=='Wildcard':
            self.sortModel.setFilterWildcard(key)
        else:
            self.sortModel.setFilterRegularExpression(key)
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet